package org.meichao.utils;

import org.meichao.constants.Constants;
import org.meichao.utils.vo.RespInfo;

/**
 * 请求信息返回工具类,简化了返回代码的编写
 * @param <T>
 */
public class RespInfoUtils<T> {

    private RespInfo<T> respInfo = new RespInfo<>();

    private RespInfoUtils() {
    }

    public static RespInfoUtils create(){
        return new RespInfoUtils<>();
    }

    /**
     * 成功:
     * return RespInfoUtils.create.respSuccess(Object).toRespInfo();
     * @param t 请求返回的Object
     * @return  返回RespInfo
     */
    public RespInfoUtils<T> respSuccess(T t) {
        respInfo.setRespCode(Constants.RespConst.OK);
        respInfo.setRespDesc(Constants.MsgConst.MSG_SUCCESS);
        respInfo.setInfo(t);
        return this;
    }

    /**
     * 失败:
     * return RespInfoUtils.create.respFailure(Object).toRespInfo();
     * @param t 返回的Object
     * @return 返回RespInfo对象
     */
    public RespInfoUtils<T> respFailure(T t) {
        respInfo.setRespCode(Constants.RespConst.ERROR);
        respInfo.setRespDesc(Constants.MsgConst.MSG_FAILURE);
        respInfo.setInfo(t);
        return this;
    }

    /**
     * @return 返回RespInfo
     */
    public RespInfo toRespInfo(){
        return respInfo;
    }

}
